{
 "cells": [
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "## 列表如何转换为ndarray",
   "id": "2ef6458c531d9a9"
  },
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2025-04-02T07:36:57.576164Z",
     "start_time": "2025-04-02T07:36:56.831780Z"
    }
   },
   "source": [
    "import numpy as np\n",
    "list1 = [1,2,3,4,5,6,7,8,9]\n",
    "arr1 = np.array(list1)\n",
    "print(type(arr1))"
   ],
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'numpy.ndarray'>\n"
     ]
    }
   ],
   "execution_count": 2
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-04-02T07:36:57.622629Z",
     "start_time": "2025-04-02T07:36:57.578136Z"
    }
   },
   "cell_type": "code",
   "source": [
    "t2 = np.array(range(10))\n",
    "print(t2)\n",
    "print(type(t2))"
   ],
   "id": "35340edc3c995f94",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0 1 2 3 4 5 6 7 8 9]\n",
      "<class 'numpy.ndarray'>\n"
     ]
    }
   ],
   "execution_count": 3
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-04-02T07:36:57.654146Z",
     "start_time": "2025-04-02T07:36:57.629630Z"
    }
   },
   "cell_type": "code",
   "source": [
    "t3 = np.arange(0,10,2)  # arange函数可以生成指定范围的数组，和range函数类似，但返回的是ndarray类型\n",
    "print(t3)\n",
    "print(type(t3))"
   ],
   "id": "68b4fc2fcc11118c",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0 2 4 6 8]\n",
      "<class 'numpy.ndarray'>\n"
     ]
    }
   ],
   "execution_count": 4
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "",
   "id": "936aeb11b273cbac"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-04-02T07:36:57.670146Z",
     "start_time": "2025-04-02T07:36:57.657144Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 二维列表转换为ndarray\n",
    "list2 = [[1,2,3],[4,5,6],[7,8,9]]\n",
    "arr2 = np.array(list2)\n",
    "print(arr2)\n",
    "print(type(arr2))"
   ],
   "id": "cd511f5e0410989f",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1 2 3]\n",
      " [4 5 6]\n",
      " [7 8 9]]\n",
      "<class 'numpy.ndarray'>\n"
     ]
    }
   ],
   "execution_count": 5
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-04-02T07:36:57.686162Z",
     "start_time": "2025-04-02T07:36:57.671144Z"
    }
   },
   "cell_type": "code",
   "source": [
    "list3 = arr2.tolist()  # 转换为列表\n",
    "print(list3)\n",
    "print(type(list3))"
   ],
   "id": "e983eef7ed647274",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1, 2, 3], [4, 5, 6], [7, 8, 9]]\n",
      "<class 'list'>\n"
     ]
    }
   ],
   "execution_count": 6
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "## 常用属性",
   "id": "742d760d34d3d8ee"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-04-02T07:36:57.702143Z",
     "start_time": "2025-04-02T07:36:57.688144Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 获取数组维度\n",
    "list3 = np.array(list3)\n",
    "print(list3.ndim)\n",
    "# 形状\n",
    "print(list3.shape)\n",
    "# 元素个数\n",
    "print(list3.size)\n",
    "# 数据类型\n",
    "print(list3[0])\n",
    "print(list3.dtype)  # int32，4字节整数，同C语言中的int类型"
   ],
   "id": "a962ed5f40032427",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2\n",
      "(3, 3)\n",
      "9\n",
      "[1 2 3]\n",
      "int32\n"
     ]
    }
   ],
   "execution_count": 7
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "## 调整数组的形状",
   "id": "a8d035793ded7f02"
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "",
   "id": "bb5e208f0f7671a7"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-04-02T07:36:57.717147Z",
     "start_time": "2025-04-02T07:36:57.704144Z"
    }
   },
   "cell_type": "code",
   "source": [
    "four  = np.array([[1,2,3], [4,5,6]])\n",
    "print(four)\n",
    "# 修改的是原有的\n",
    "four1 = four\n",
    "print(id(four))"
   ],
   "id": "e5cb5f0dd47e6328",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1 2 3]\n",
      " [4 5 6]]\n",
      "2975912027984\n"
     ]
    }
   ],
   "execution_count": 8
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-04-02T07:36:57.748659Z",
     "start_time": "2025-04-02T07:36:57.720149Z"
    }
   },
   "cell_type": "code",
   "source": [
    "four.shape = (3,2)  # 调整形状\n",
    "print(id(four))\n",
    "print(id(four1))\n",
    "print(four)"
   ],
   "id": "48ce4b803304e429",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2975912027984\n",
      "2975912027984\n",
      "[[1 2]\n",
      " [3 4]\n",
      " [5 6]]\n"
     ]
    }
   ],
   "execution_count": 9
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-04-02T07:36:57.779659Z",
     "start_time": "2025-04-02T07:36:57.751662Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 将多维数组转换为一维数组\n",
    "five = four.reshape((6,), order='C')\n",
    "# 默认情况下，reshape函数会按照C语言的内存顺序排列元素，即按行优先，列优先则需要指定order='F'（Fortran语言的内存顺序）\n",
    "six= four.flatten()  # 展平数组\n",
    "print(five)\n",
    "print(six)\n",
    "\n",
    "print(five.reshape(3,2))  # 还原为原来的形状"
   ],
   "id": "4f2190fc5b1dde3d",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1 2 3 4 5 6]\n",
      "[1 2 3 4 5 6]\n",
      "[[1 2]\n",
      " [3 4]\n",
      " [5 6]]\n"
     ]
    }
   ],
   "execution_count": 10
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-04-02T07:36:57.794889Z",
     "start_time": "2025-04-02T07:36:57.784660Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 增加维度\n",
    "seven = five.reshape(1,3,2)\n",
    "print(seven)"
   ],
   "id": "bef1ec19fafdc32f",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[[1 2]\n",
      "  [3 4]\n",
      "  [5 6]]]\n"
     ]
    }
   ],
   "execution_count": 11
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "## 改变数据类型",
   "id": "75114e907be30ab4"
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "",
   "id": "33168ab25d16e58c"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-04-02T07:36:57.810961Z",
     "start_time": "2025-04-02T07:36:57.796891Z"
    }
   },
   "cell_type": "code",
   "source": [
    "\n",
    "f = np.array([1,2,3],dtype=np.int16)\n",
    "print(f.itemsize)\n",
    "print(f.nbytes)  # 字节数\n",
    "print(f.dtype)"
   ],
   "id": "de381fdde4ef13e3",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2\n",
      "6\n",
      "int16\n"
     ]
    }
   ],
   "execution_count": 12
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-04-02T07:36:57.826969Z",
     "start_time": "2025-04-02T07:36:57.812941Z"
    }
   },
   "cell_type": "code",
   "source": [
    "import random\n",
    "arr = np.array([random.random() for i in range(10)])\n",
    "print(np.round(arr,2))"
   ],
   "id": "79c08d3e9b57ea02",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0.44 0.75 0.15 0.4  0.98 0.29 0.38 0.5  0.54 0.28]\n"
     ]
    }
   ],
   "execution_count": 13
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "## 练习轴",
   "id": "70ab32e7147ec497"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-04-02T07:36:57.842465Z",
     "start_time": "2025-04-02T07:36:57.828946Z"
    }
   },
   "cell_type": "code",
   "source": [
    "a = np.array([[1,2,3],[4,5,6],[7,8,9]])\n",
    "print(a.sum(axis=0))  # 按列求和\n",
    "print(a.sum(axis=1))  # 按行求和\n",
    "print(a.mean(axis=0))  # 按列求均值\n",
    "print(a.mean(axis=1))  # 按行求均值"
   ],
   "id": "897cc9e82d38329",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[12 15 18]\n",
      "[ 6 15 24]\n",
      "[4. 5. 6.]\n",
      "[2. 5. 8.]\n"
     ]
    }
   ],
   "execution_count": 14
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "## 索引和切片",
   "id": "8781cbba6618dc8f"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-04-02T07:36:57.858459Z",
     "start_time": "2025-04-02T07:36:57.845459Z"
    }
   },
   "cell_type": "code",
   "source": [
    "a = np.arange(10)\n",
    "print(a[0], a)\n",
    "print(a[2:])# 表示从该索引开始以后的所有项都将被提取\n",
    "print(a[2:8:2])# 切片是左闭右开\n"
   ],
   "id": "7d898ac054d8ce3d",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0 [0 1 2 3 4 5 6 7 8 9]\n",
      "[2 3 4 5 6 7 8 9]\n",
      "[2 4 6]\n"
     ]
    }
   ],
   "execution_count": 15
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-04-02T07:36:57.873899Z",
     "start_time": "2025-04-02T07:36:57.859459Z"
    }
   },
   "cell_type": "code",
   "source": [
    "t1 = np.arange(24).reshape(4,6)  # 数组的形状\n",
    "print(t1)           # 打印数组\n",
    "print(t1[1])        # 取第2行\n",
    "print(t1[1:])       # 取连续的多列\n",
    "print(t1[1:3,:])    # 取连续的多行\n",
    "print(t1[[0,2,3]])  # 取不连续的多行\n",
    "print(t1[[0,1,1],[0,1,3]])\n"
   ],
   "id": "93713e0d3c0f99a5",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0  1  2  3  4  5]\n",
      " [ 6  7  8  9 10 11]\n",
      " [12 13 14 15 16 17]\n",
      " [18 19 20 21 22 23]]\n",
      "[ 6  7  8  9 10 11]\n",
      "[[ 6  7  8  9 10 11]\n",
      " [12 13 14 15 16 17]\n",
      " [18 19 20 21 22 23]]\n",
      "[[ 6  7  8  9 10 11]\n",
      " [12 13 14 15 16 17]]\n",
      "[[ 0  1  2  3  4  5]\n",
      " [12 13 14 15 16 17]\n",
      " [18 19 20 21 22 23]]\n",
      "[0 7 9]\n"
     ]
    }
   ],
   "execution_count": 16
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "## 条件选择",
   "id": "869f085179f09202"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-04-02T07:36:57.888899Z",
     "start_time": "2025-04-02T07:36:57.874900Z"
    }
   },
   "cell_type": "code",
   "source": [
    "t1[t1>5]=0\n",
    "print(t1)\n",
    "# 条件与运算\n",
    "t2 = t1[t1>5]\n",
    "print(t2)\n",
    "# 条件非运算\n",
    "t3 = t1[~(t1<=5)]\n",
    "# 条件或运算\n",
    "t4 = t1[(t1>5) | (t1<2)]\n",
    "print(t4)\n",
    "# clip\n"
   ],
   "id": "350bfe8aa7a6a631",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[0 1 2 3 4 5]\n",
      " [0 0 0 0 0 0]\n",
      " [0 0 0 0 0 0]\n",
      " [0 0 0 0 0 0]]\n",
      "[]\n",
      "[0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]\n"
     ]
    }
   ],
   "execution_count": 17
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-04-02T07:36:57.904667Z",
     "start_time": "2025-04-02T07:36:57.889900Z"
    }
   },
   "cell_type": "code",
   "source": [
    "t5 = np.clip(t1,2,7)\n",
    "print(t5)\n",
    "# 也可以这样写\n",
    "t5 = t1.clip(2,7)\n",
    "print(t5)\n",
    "# 小于2的元素变为2，大于7的元素变为7，其余元素不变"
   ],
   "id": "b860333b2ccb1276",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[2 2 2 3 4 5]\n",
      " [2 2 2 2 2 2]\n",
      " [2 2 2 2 2 2]\n",
      " [2 2 2 2 2 2]]\n",
      "[[2 2 2 3 4 5]\n",
      " [2 2 2 2 2 2]\n",
      " [2 2 2 2 2 2]\n",
      " [2 2 2 2 2 2]]\n"
     ]
    }
   ],
   "execution_count": 18
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "## 三目运算符",
   "id": "f0451c29df35e4dd"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-04-02T07:36:57.920232Z",
     "start_time": "2025-04-02T07:36:57.906181Z"
    }
   },
   "cell_type": "code",
   "source": [
    "score = np.array([[80,88],[82,81],[75,81]])\n",
    "print(score)\n",
    "result = np.where(score<80,True,False)# 条件选择，类似于if else语句\n",
    "print(result)\n",
    "score[result] = 100 # 条件修改，可以将所有为True的元素修改为100，两个数组的shape必须相同\n",
    "print(score)\n"
   ],
   "id": "d494fd0355c716cc",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[80 88]\n",
      " [82 81]\n",
      " [75 81]]\n",
      "[[False False]\n",
      " [False False]\n",
      " [ True False]]\n",
      "[[ 80  88]\n",
      " [ 82  81]\n",
      " [100  81]]\n"
     ]
    }
   ],
   "execution_count": 19
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "## 添加删除去重",
   "id": "dab7b7dd741b40c8"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-04-02T07:36:57.935758Z",
     "start_time": "2025-04-02T07:36:57.922231Z"
    }
   },
   "cell_type": "code",
   "source": [
    "a =np.array([[1,2,3],[4,5,6]])\n",
    "print(np.append(a,[[7,8,9]],axis=0)) #如果未输入axis，ndarray会被展开\n",
    "print(f'append处理结果↑')# 原数组不变\n",
    "print(np.insert(a,1,[7,8,9],axis=0))    # axis指定行，1表示插入到第2行\n",
    "print(f'insert处理结果↑')    # 原数组不变\n",
    "print(np.delete(a,1,axis=0))    # axis指定行，1表示删除第2行\n",
    "print(f'delete处理结果↑')    # 原数组不变\n",
    "print(np.unique(a))    # 去重\n",
    "print(f'unique处理结果↑')\n",
    "print(np.insert(a,1,11,axis=1))\n",
    "print(f'insert处理结果,广播↑')\n",
    "print(np.append(a,[[5,5,5],[7,8,9]],axis=1))\n",
    "print(f'append处理结果,给列增加元素↑')"
   ],
   "id": "eef8206737d7c169",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1 2 3]\n",
      " [4 5 6]\n",
      " [7 8 9]]\n",
      "append处理结果↑\n",
      "[[1 2 3]\n",
      " [7 8 9]\n",
      " [4 5 6]]\n",
      "insert处理结果↑\n",
      "[[1 2 3]]\n",
      "delete处理结果↑\n",
      "[1 2 3 4 5 6]\n",
      "unique处理结果↑\n",
      "[[ 1 11  2  3]\n",
      " [ 4 11  5  6]]\n",
      "insert处理结果,广播↑\n",
      "[[1 2 3 5 5 5]\n",
      " [4 5 6 7 8 9]]\n",
      "append处理结果,给列增加元素↑\n"
     ]
    }
   ],
   "execution_count": 20
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-04-02T07:36:57.951775Z",
     "start_time": "2025-04-02T07:36:57.937755Z"
    }
   },
   "cell_type": "code",
   "source": [
    "u, indices, counts= np.unique(a,return_inverse=True,return_counts=True)\n",
    "print(u)    # 去重后数组\n",
    "print(indices)  # 原数组的索引\n",
    "print(counts)    # 每个元素出现的次数"
   ],
   "id": "1d0e0bc62db64e6b",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1 2 3 4 5 6]\n",
      "[0 1 2 3 4 5]\n",
      "[1 1 1 1 1 1]\n"
     ]
    }
   ],
   "execution_count": 21
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "## numpy的计算",
   "id": "25a1652b8209e312"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-04-02T07:47:36.717328Z",
     "start_time": "2025-04-02T07:47:36.697059Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# numpy的计算函数非常多，可以在课件中查看\n",
    "ndarr1 = np.arange(24)\n",
    "transition_list = [round(random.random()*10,2) for _ in range(24)]\n",
    "print(transition_list)\n",
    "ndarr2 = np.array(transition_list).reshape(4,6)\n",
    "one, two = np.modf(ndarr2)  # modf方法可以将矩阵中的整数部分和小数部分分开\n",
    "print('这是one',one)\n",
    "print('这是two',two)\n"
   ],
   "id": "802038deae1fd589",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[3.99, 4.81, 6.75, 6.51, 1.36, 7.13, 0.01, 2.03, 2.43, 6.88, 1.48, 1.64, 8.48, 7.02, 4.14, 0.86, 4.78, 1.26, 4.92, 0.67, 2.86, 6.67, 7.57, 6.94]\n",
      "这是one [[0.99 0.81 0.75 0.51 0.36 0.13]\n",
      " [0.01 0.03 0.43 0.88 0.48 0.64]\n",
      " [0.48 0.02 0.14 0.86 0.78 0.26]\n",
      " [0.92 0.67 0.86 0.67 0.57 0.94]]\n",
      "这是two [[3. 4. 6. 6. 1. 7.]\n",
      " [0. 2. 2. 6. 1. 1.]\n",
      " [8. 7. 4. 0. 4. 1.]\n",
      " [4. 0. 2. 6. 7. 6.]]\n"
     ]
    }
   ],
   "execution_count": 31
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-04-02T08:07:07.718862Z",
     "start_time": "2025-04-02T08:07:07.712847Z"
    }
   },
   "cell_type": "code",
   "source": [
    "score = np.array([[80,88],[82,81],[75,81]])\n",
    "result = np.argmin(score,axis=0)  # 按列求最小值索引\n",
    "res = np.min(score,axis=0) # 这样我们可以知道最小的81是第二排的，是从前往后遍历的\n",
    "\n",
    "print(result)\n",
    "print(res)"
   ],
   "id": "f770d143b2b44533",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[2 1]\n",
      "[75 81]\n"
     ]
    }
   ],
   "execution_count": 32
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "## 数组的拼接",
   "id": "a85325ef8a3f7cae"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-04-02T07:36:57.982759Z",
     "start_time": "2025-04-02T07:36:57.969757Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# concatenate是拼接，不会增加维度\n",
    "a = np.array([[1,2,3],[4,5,6]])\n",
    "b = np.array([[7,8,9],[10,11,12]])\n",
    "print(np.concatenate((a,b),axis=0))  # 按行拼接\n",
    "print(np.concatenate((a,b),axis=1))  # 按列拼接\n",
    "# stack是堆叠，会增加一个维度\n",
    "print('-'*50)\n",
    "print(np.stack((a,b),axis=0))        # 按行拼接\n",
    "print('-'*50)\n",
    "print(np.stack((a,b),axis=1))        # 按列拼接\n",
    "'''\n",
    "stack函数可以将多个数组拼接在一起，axis=0表示按行拼接，axis=1表示按列拼接，堆叠，会增加一个维度\n",
    "concatenate函数可以将多个数组按指定轴拼接在一起，axis=0表示按行拼接，axis=1表示按列拼接\n",
    "按哪个轴堆叠就增加哪个轴的维度\n",
    "'''"
   ],
   "id": "930c1adcb63b9e7f",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 1  2  3]\n",
      " [ 4  5  6]\n",
      " [ 7  8  9]\n",
      " [10 11 12]]\n",
      "[[ 1  2  3  7  8  9]\n",
      " [ 4  5  6 10 11 12]]\n",
      "--------------------------------------------------\n",
      "[[[ 1  2  3]\n",
      "  [ 4  5  6]]\n",
      "\n",
      " [[ 7  8  9]\n",
      "  [10 11 12]]]\n",
      "--------------------------------------------------\n",
      "[[[ 1  2  3]\n",
      "  [ 7  8  9]]\n",
      "\n",
      " [[ 4  5  6]\n",
      "  [10 11 12]]]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "'\\nstack函数可以将多个数组拼接在一起，axis=0表示按行拼接，axis=1表示按列拼接，堆叠，会增加一个维度\\nconcatenate函数可以将多个数组按指定轴拼接在一起，axis=0表示按行拼接，axis=1表示按列拼接\\n'"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 22
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "",
   "id": "dab9b2ed93ccaf09"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-04-02T07:36:57.998480Z",
     "start_time": "2025-04-02T07:36:57.983755Z"
    }
   },
   "cell_type": "code",
   "source": [
    "#分割数组\n",
    "a = np.array([1,2,3,4,5,6,7,8,9,10,11,12]).reshape(4,3)\n",
    "print(a)\n",
    "print(np.split(a,3,axis=1))\n",
    "print('-'*50)\n",
    "print(np.split(a,4,axis=0))"
   ],
   "id": "2ecf76de71c3b033",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 1  2  3]\n",
      " [ 4  5  6]\n",
      " [ 7  8  9]\n",
      " [10 11 12]]\n",
      "[array([[ 1],\n",
      "       [ 4],\n",
      "       [ 7],\n",
      "       [10]]), array([[ 2],\n",
      "       [ 5],\n",
      "       [ 8],\n",
      "       [11]]), array([[ 3],\n",
      "       [ 6],\n",
      "       [ 9],\n",
      "       [12]])]\n",
      "--------------------------------------------------\n",
      "[array([[1, 2, 3]]), array([[4, 5, 6]]), array([[7, 8, 9]]), array([[10, 11, 12]])]\n"
     ]
    }
   ],
   "execution_count": 23
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "",
   "id": "d71b0e5366f2a5ef"
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "## NaN和inf",
   "id": "c516ed054d684f55"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-04-02T07:36:58.013523Z",
     "start_time": "2025-04-02T07:36:58.000376Z"
    }
   },
   "cell_type": "code",
   "source": [
    "a = np.arange(24, dtype=np.float64).reshape(4,6)\n",
    "a[2,3], a[2,4] = np.nan, np.nan\n",
    "print(a)"
   ],
   "id": "a7e506a277f1bbbd",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0.  1.  2.  3.  4.  5.]\n",
      " [ 6.  7.  8.  9. 10. 11.]\n",
      " [12. 13. 14. nan nan 17.]\n",
      " [18. 19. 20. 21. 22. 23.]]\n"
     ]
    }
   ],
   "execution_count": 24
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-04-02T07:36:58.028540Z",
     "start_time": "2025-04-02T07:36:58.014535Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# nan和nan是不相等的，这有助于数据处理\n",
    "print(np.nan == np.nan)"
   ],
   "id": "82355fa54a124950",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "False\n"
     ]
    }
   ],
   "execution_count": 25
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-04-02T07:36:58.044085Z",
     "start_time": "2025-04-02T07:36:58.030541Z"
    }
   },
   "cell_type": "code",
   "source": [
    "print(a!= a)\n",
    "print('-'*50)\n",
    "print(np.count_nonzero(a!= a))  # 非零元素的个数\n",
    "a[np.isnan(a)] = 0\n",
    "print(a)"
   ],
   "id": "339b2e3c4da1b4b6",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[False False False False False False]\n",
      " [False False False False False False]\n",
      " [False False False  True  True False]\n",
      " [False False False False False False]]\n",
      "--------------------------------------------------\n",
      "2\n",
      "[[ 0.  1.  2.  3.  4.  5.]\n",
      " [ 6.  7.  8.  9. 10. 11.]\n",
      " [12. 13. 14.  0.  0. 17.]\n",
      " [18. 19. 20. 21. 22. 23.]]\n"
     ]
    }
   ],
   "execution_count": 26
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-04-02T07:36:58.060062Z",
     "start_time": "2025-04-02T07:36:58.046062Z"
    }
   },
   "cell_type": "code",
   "source": [
    "test1 = np.arange(24, dtype=np.float64).reshape(4,6)\n",
    "print(test1)\n",
    "print(test1[:,2])"
   ],
   "id": "4ba6a59e8f926d74",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0.  1.  2.  3.  4.  5.]\n",
      " [ 6.  7.  8.  9. 10. 11.]\n",
      " [12. 13. 14. 15. 16. 17.]\n",
      " [18. 19. 20. 21. 22. 23.]]\n",
      "[ 2.  8. 14. 20.]\n"
     ]
    }
   ],
   "execution_count": 27
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-04-02T07:36:58.075336Z",
     "start_time": "2025-04-02T07:36:58.063072Z"
    }
   },
   "cell_type": "code",
   "source": "",
   "id": "3ef8f931148d1a47",
   "outputs": [],
   "execution_count": 27
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "DL",
   "language": "python",
   "name": "dl"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
